package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.CheckDetailed;
import com.ruoyi.system.domain.MedicalRecord;
import com.ruoyi.system.domain.PatientCosts;
import com.ruoyi.system.domain.Prescription;
import com.ruoyi.system.service.IPatientCostsService;
import com.ruoyi.system.service.impl.CheckDetailedServiceImpl;
import com.ruoyi.system.service.impl.MedicalRecordServiceImpl;
import com.ruoyi.system.service.impl.PrescriptionServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 患者消费明细Controller
 *
 * @author ruoyi
 * @date 2023-07-04
 */
@Api(tags = "患者消费明细模块")
@RestController
@RequestMapping("/system/PatientCosts")
public class PatientCostsController extends BaseController {
    @Autowired
    private IPatientCostsService patientCostsService;

    @Autowired
    private MedicalRecordServiceImpl medicalRecordService;


    @Autowired
    private CheckDetailedServiceImpl checkDetailedService;

    @Autowired
    private PrescriptionServiceImpl prescriptionService;

    /**
     * 查询患者消费明细列表
     */
    @ApiOperation(value = "查询患者消费明细列表")
    @PreAuthorize("@ss.hasPermi('system:PatientCosts:list')")
    @GetMapping("/list")
    public Page<PatientCosts> list(PatientCosts entity, Page<PatientCosts> page) {

        QueryWrapper<PatientCosts> wrapper = new QueryWrapper<>();
        if (entity.getRegisterId() != null) {
            wrapper.eq("RegisterId", entity.getRegisterId());
        }
        if (entity.getItemId() != null) {
            wrapper.eq("ItemId", entity.getItemId());
        }
        if (entity.getState() != null) {
            wrapper.eq("State", entity.getState());
        }
        return patientCostsService.page(page, wrapper);
    }

    /**
     * 导出患者消费明细列表
     */
    @ApiOperation(value = "导出患者消费明细列表")
    @PreAuthorize("@ss.hasPermi('system:PatientCosts:export')")
    @Log(title = "患者消费明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PatientCosts patientCosts) {
        LambdaQueryWrapper<PatientCosts> queryWrapper = new LambdaQueryWrapper<>();
        List<PatientCosts> list = patientCostsService.list(queryWrapper);
        ExcelUtil<PatientCosts> util = new ExcelUtil<PatientCosts>(PatientCosts.class);
        util.exportExcel(response, list, "患者消费明细数据");
    }

    /**
     * 更改缴费状况
     */
    @ApiOperation(value = "更改缴费状况")
    @GetMapping("/edit")
    public AjaxResult edit(@RequestParam("Id") Long Id) {
        UpdateWrapper<PatientCosts> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("Id", Id).set("State", 1L);
        PatientCosts patientCosts = patientCostsService.getById(Id);
        QueryWrapper<MedicalRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("RegisterId", patientCosts.getRegisterId());
        if (patientCosts.getItemId() == 2) {
            UpdateWrapper<CheckDetailed> cWrapper = new UpdateWrapper<>();
            cWrapper.eq("MedicalId", medicalRecordService.getOne(wrapper).getId()).set("CheckState", 2L);
            checkDetailedService.update(cWrapper);
        }
        if (patientCosts.getItemId() == 3) {
            UpdateWrapper<Prescription> pWrapper = new UpdateWrapper<>();
            pWrapper.eq("MedicalId", medicalRecordService.getOne(wrapper).getId()).set("PrescriptionState", 3L);
            prescriptionService.update(pWrapper);
        }
        return toAjax(patientCostsService.update(null, updateWrapper));
    }

    /**
     * 获取患者消费明细详细信息
     */
    @ApiOperation(value = "获取患者消费明细详细信息")
    @PreAuthorize("@ss.hasPermi('system:PatientCosts:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(patientCostsService.getById(id));
    }

    /**
     * 新增患者消费明细
     */
    @ApiOperation(value = "新增患者消费明细")
    @PreAuthorize("@ss.hasPermi('system:PatientCosts:add')")
    @Log(title = "患者消费明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PatientCosts patientCosts) {
        return toAjax(patientCostsService.save(patientCosts));
    }

    /**
     * 修改患者消费明细
     */
    @ApiOperation(value = "修改患者消费明细")
    @PreAuthorize("@ss.hasPermi('system:PatientCosts:edit')")
    @Log(title = "患者消费明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PatientCosts patientCosts) {
        return toAjax(patientCostsService.updateById(patientCosts));
    }


    /**
     * 删除患者消费明细
     */
    @ApiOperation(value = "删除患者消费明细")
    @PreAuthorize("@ss.hasPermi('system:PatientCosts:remove')")
    @Log(title = "患者消费明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids) {
        return toAjax(patientCostsService.removeBatchByIds(Arrays.asList(Ids)));
    }
}
